$(document).ready(function() {
	// To Top
	$().UItoTop({ easingType: 'easeOutQuart' });
	
	//Caledar
	$("#caledar_from").datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'dd-mm-yy',
		yearRange: '1930:2050'
	});
	$("#caledar_to").datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'dd-mm-yy',
		yearRange: '1930:2050'
	});
	
	//Custom Form
	$('.custom_chk').jqTransCheckBox();
	$('.custom_rd').jqTransRadio();
	$('.custom_select').jqTransSelect();
	
	//FancyBox
	$('.fancyboxClick').fancybox();
	
	//Sub Menu
	$('.activeSubMenu a').click(function(){
		$(this).addClass('active');
		$(this).parent().find('.sub').slideToggle();
	});
	
	//Show Loading
	$('#saveContent').click(function(){
		$('#loader').fadeIn();
	});
	
	//Fix width Permission Denied
	$('.gr_perm_error').width($('.right_content').width()-2);
	$('.gr_perm_success').width($('.right_content').width()-2);
	
	//Fix Enter input
	$('#frmManagement input').keypress(function(event){
		if(event.which == 13){
			save();
			return false;
		}
	})
	
	//Parse URL
	var url = $.url(document.location.href);
	if(url.segment(-1)!='update' && url.segment(-2)!='update'){
		if(url.fsegment(1)=='back' || url.fsegment(1)=='save'){
			if(url.fsegment(1)=='save'){
				show_perm_success();
			}
			if(url.segment(-1)!='update_profile' && url.segment(-1)!='setting'){
				if($('#start').val()==''){
					$('#start').val(0);
				}
				searchContent($('#start').val(),10);
			}
		}else{
			if(url.segment(-1)!='update_profile' && url.segment(-1)!='setting'){
				//Load Content
				if(module!='admincp'){
					searchContent(0,10);
				}
			}
		}
	}
});

function show_perm_denied(){
	$('.gr_perm_error').fadeIn(500);
	$('#loader').fadeOut(300);
	$('.table').css('marginTop',4);
	setTimeout("$('.gr_perm_error').fadeOut(300); $('.table').css('marginTop',0);",5000);
}

function show_perm_success(){
	$('.gr_perm_success').fadeIn(500);
	$('#loader').fadeOut(300);
	$('.table').css('marginTop',4);
	setTimeout("$('.gr_perm_success').fadeOut(300); $('.table').css('marginTop',0);",5000);
}

function searchContent(start,per_page){
	if(per_page==undefined){
		if($('#per_page').val()){
			per_page = $('#per_page').val();
		}else{
			per_page = 10;
		}
	}
	var func_sort = $('#func_sort').val();
	var type_sort = $('#type_sort').val();
	$('#start').val(start);
	$.post(root+'admincp/'+module+'/ajaxLoadContent',{
		func_order_by: func_sort,
		order_by: type_sort,
		start: start,
		per_page: per_page,
		dateFrom: $('#caledar_from').val(),
		dateTo: $('#caledar_to').val(),
		content: $('#search_content').val()
	},function(data){
		$('#ajax_loadContent').html(data);

		//Custom Form
		$('.custom_chk').jqTransCheckBox();
		
		//FancyBox
		$('.fancyboxClick').fancybox();
		
		//Set Icon Order By
		$('.sort').removeClass('icon_sort_desc');
		$('.sort').removeClass('icon_sort_asc');
		$('.sort').addClass('icon_no_sort');
		if(type_sort=='DESC'){
			$('#'+func_sort).addClass('icon_sort_desc');
		}else{
			$('#'+func_sort).addClass('icon_sort_asc');
		}
	});
}

function enterSearch(e){
	if (e.keyCode == 13){ 
		searchContent(0);
	}
}

function sort(func){
	var func_sort = $('#func_sort').val();
	var type_sort = $('#type_sort').val();
	if(func==func_sort){
		if(type_sort=='DESC'){
			$('#type_sort').val('ASC');
		}else{
			$('#type_sort').val('DESC');
		}
	}else{
		$('#func_sort').val(func)
		$('#type_sort').val('DESC');
	}
	searchContent(0,$('#per_page').val());
}

function updateStatus(id,status,module){
	var url = root+'admincp/'+module+'/ajaxUpdateStatus';
	$.post(url,{
			id: id,
			status: status
		},
		function(data){
			$('#loadStatusID_'+id).html(data);
			if(module=='admincp_modules'){
				$.get(root+'admincp/menu', function(data){
					$('#loadMenu').html(data);
				});
			}
		}
	);
}

function selectItem(id){
	var itemCheck = document.getElementById('item'+id);
	if(itemCheck.checked==false){
		$('.item_row'+id).addClass('row_active');
	}else{
		$('.item_row'+id).removeClass('row_active');
	}
}

function selectAllItems(max){
	if(document.getElementById('selectAllItems').checked==false){
		$('.jqTransformCheckboxWrapper a').addClass('jqTransformChecked');
		for(var i=0;i<max;i++){
			if(document.getElementById('item'+i)!=null){
				$('.item_row'+i).addClass('row_active');
				itemCheck = document.getElementById('item'+i);
				itemCheck.checked = true;
			}
		}
	}else{
		$('.jqTransformCheckboxWrapper a').removeClass('jqTransformChecked');
		for(var i=0;i<max;i++){
			if(document.getElementById('item'+i)!=null){
				$('.item_row'+i).removeClass('row_active');
				itemCheck = document.getElementById('item'+i);
				itemCheck.checked = false;
			}
		}
	}
}

function showStatusAll(){
	var max = $('#per_page').val();
	for(var i=0;i<max;i++){
		if(document.getElementById('item'+i)!=null){
			if(document.getElementById('item'+i).checked==true){
				updateStatus($('#item'+i).val(),0,module);
			}
		}
	}
}

function hideStatusAll(){
	var max = $('#per_page').val();
	for(var i=0;i<max;i++){
		if(document.getElementById('item'+i)!=null){
			if(document.getElementById('item'+i).checked==true){
				updateStatus($('#item'+i).val(),1,module);
			}
		}
	}
}

function deleteItem(id){
	var del = confirm("Are you sure delete item?");
	if(del){
		var url = root+'admincp/'+module+'/delete';
		$.post(url,{
				id: id
			},function(data){
				if(data=='permission-denied'){
					show_perm_denied();
				}else{
					searchContent($('#start').val(),$('#per_page').val());
				}
			}
		);
	}
}

function deleteAll(){
	var del = confirm("Are you sure delete item selected?");
	if(del){
		var max = $('#per_page').val();
		for(var i=0;i<max;i++){
			if(document.getElementById('item'+i)!=null){
				if(document.getElementById('item'+i).checked==true){
					id = $('#item'+i).val();
					var url = root+'admincp/'+module+'/delete';
					$.post(url,{
						id: id
					},function(data){
						if(data=='permission-denied'){
							show_perm_denied();
						}else{
							searchContent($('#start').val(),$('#per_page').val());
						}
					});
				}
			}
		}
	}
}

function chk_perm(id,perm){
	if(perm!='no_access'){
		if(perm=='read'){
			if($('#read'+id).attr('checked')=='checked'){
				$('#noaccess'+id).attr("checked",true);
				$('#write'+id).attr("checked",false);
				$('#delete'+id).attr("checked",false);
				$('.custom_noaccess'+id).addClass('jqTransformChecked');
				$('.custom_write'+id).removeClass('jqTransformChecked');
				$('.custom_delete'+id).removeClass('jqTransformChecked');
			}else{
				$('#noaccess'+id).attr("checked",false);
				$('.custom_noaccess'+id).removeClass('jqTransformChecked');
			}
		}else{
			$('#read'+id).attr("checked",true);
			$('#noaccess'+id).attr("checked",false);
			$('.custom_read'+id).addClass('jqTransformChecked');
			$('.custom_noaccess'+id).removeClass('jqTransformChecked');
		}
	}else{
		if($('#noaccess'+id).attr('checked')=='checked'){
			$('#read'+id).attr("checked",true);
			$('.custom_read'+id).addClass('jqTransformChecked');
		}else{
			$('.perm_access'+id).attr("checked",false);
			$('.custom_read'+id).removeClass('jqTransformChecked');
			$('.custom_write'+id).removeClass('jqTransformChecked');
			$('.custom_delete'+id).removeClass('jqTransformChecked');
		}
	}
}